//声明一个域
var app = angular.module("editApp", []);
//从主页面接收参数
var keywords_type = UI.getParameterByName('keywords_type');
var keywords_id = UI.getParameterByName('keywords_id');
var keywords_seek = UI.getParameterByName('keywords_seek');
//keyword_seek = decodeURI(keywords_seek);
//keywords_type = decodeURI(keywords_type);
if(keywords_type == '百度'){
	keywords_type = 1;
}else if(keywords_type == 'MetaSearch'){
	keywords_type = 0;
}
//向后台传输数据格式设置
app.config(function($httpProvider) {
	$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
	$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
//	$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
	$httpProvider.transformRequest=function(obj) {    
        var str = [];    
        for (var p in obj) {    
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));    
        }    
        return str.join("&");    
    }  
});
app.controller('editCtrl', function($scope, $http, $window, $timeout) {
	//这个VM对象是给页面的
	var vm = $scope.vm = {
		keywords_type : keywords_type,
		keywords_id : keywords_id,
		keywords_seek : keywords_seek
	}
// 	声明一个数组来接收复选框的值
//	$scope.selected = [];
	var avoidDup = true;
	// 点击确定方法
	$scope.Update = function() {
		if(avoidDup==false){
			return false;
		}
		$scope.keywords_type = $("#match_title").attr('class').search('checked') >= 0 ? '百度' : null;
//		if($scope.keywords_type!=null){
//			$scope.selected.push($scope.keywords_type);
//		}
//		var keywords_type = $scope.selected.join(",");
//		var keyword_t = keywords_type.toString().substr(0,1)==',' ? keywords_type.toString().substr(1) : keywords_type.toString();
		var vm1 = {
			//这是从页面输入框读取的
			"keywords_seek" : vm.keywords_seek,
			"keywords_type" : $scope.keywords_type,
			"keywords_id" : keywords_id,
		};
		if(vm1.keywords_seek!=undefined){
			avoidDup =false;
			$.post(basePath + 'Keyword/modifyKeyword.do', vm1).success(function(data) {
				if (data.returnCode == 10000) {
					layer.msg(data.returnMessage, 1, 10);
					$timeout(function(){
						try {
							window.parent.$(".dtab").find('#jsgo a.cur').trigger('click');
						} catch(e) {}
						window.parent.layer.closeAll();
					},1000);
				} else if (data.returnCode == 999994) {
					setUserTimeout(data);
				} else {
					layer.msg(data.returnMessage, 2, -1);
					avoidDup = true;
					return false;
				}
			});
		}
	}
    $scope.isChecked = function(e, isCheck) {
        var _ = $(e);
        if(isCheck == 0 || isCheck == 1) {
            if(isCheck == 0) {
                _.removeClass('checked');
            } else {
                _.addClass('checked');
            }
        } else {
            if(_.attr('class').search('checked') > -1) {
                _.removeClass('checked');
            } else {
                _.addClass('checked');
            }
        }
    }
    $scope.isChecked($("#match_title"), keywords_type);
	//取消关闭弹窗
	$scope.close = function(e){
		window.parent.layer.closeAll();
	}
});